package com.tjdr.his.drugstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tjdr.his.domain.entity.DrugInfo;
import com.tjdr.his.domain.entity.Prescription;
import com.tjdr.his.domain.entity.Register;
import com.tjdr.his.domain.mapper.IRegisterMapper;
import com.tjdr.his.drugstore.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/10 14:57
 * @DESCRIBER
 */
@Service
public class IRegisterServiceImpl implements IRegisterService {
    @Autowired
    private IRegisterMapper mapper;
    @Cacheable(cacheNames = "RegisterCache")
    @Override
    public IPage getRegisterByCondtion(String realName, String caseNumber, Integer pageSize, Integer currentPage) {
        IPage<Register> page = new Page<>();
        page.setCurrent(currentPage);//当前页
        page.setSize(pageSize);//设置每页显示的数量

        LambdaQueryWrapper<Register> queryWrapper = new QueryWrapper<Register>().lambda();
        //病历号是否为空
        if(caseNumber!=null && !"".equals(caseNumber.trim())){
            queryWrapper.eq(Register::getCaseNumber,caseNumber);
            //姓名不是空
            if(realName!=null && !"".equals(realName.trim())){
                //根据病历号和姓名查询
                queryWrapper.like(Register::getRealName,realName);
            }
            //姓名是空，根据病历精确查询
            return mapper.selectPage(page,queryWrapper);

//            mapper.selectPage(page,queryWrapper).getTotal();

        }else{
            //病历号为空了
            if(realName!=null && !"".equals(realName.trim())){
                //姓名不为空,根据姓名模糊查找
                queryWrapper.like(Register::getRealName,realName);
                return  mapper.selectPage(page,queryWrapper);
            }
            //姓名为空
            return   mapper.selectPage(page,null);
        }
    }


    @CacheEvict(cacheNames = "RegisterCache", allEntries = true)
    @Override
    public boolean deleteRegisterByCaseNumber(String caseNumber) {
        if(caseNumber!=null && !"".equals(caseNumber.trim())){
            Map map  = new HashMap<>();
            map.put("case_number",caseNumber);
            int count =  mapper.deleteByMap(map);
            return count>0?true:false;
        }
        return false;
    }

    @Override
    public IPage getRegisterPresDrug(int id, int pageSize, int currentPage,String drugState) {

        IPage<Map<String,Object>> page = new Page<>();
        page.setCurrent(currentPage);//当前页
        page.setSize(pageSize);//设置每页显示的数量

        MPJLambdaWrapper<Register> mpjLambdaWrapper = new MPJLambdaWrapper();
        mpjLambdaWrapper.eq(Register::getId,Integer.valueOf(id));
        mpjLambdaWrapper.eq(Prescription::getDrugState,"drugState");
        mpjLambdaWrapper.select(Register::getId);
        mpjLambdaWrapper.selectAll(DrugInfo.class);//查询出DrugInfo的所有字段
        mpjLambdaWrapper.selectAll(Prescription.class);//查询出DrugInfo的所有字段

        mpjLambdaWrapper.innerJoin(Prescription.class,Prescription::getRegisterId,Register::getId);
        mpjLambdaWrapper.innerJoin(DrugInfo.class,DrugInfo::getId,Prescription::getDrugId);
        return mapper.selectJoinMapsPage(page,mpjLambdaWrapper);


    }


}
